package BehaviorModel.mediatorModel;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述: 具体的中介者
 * 2024/02/20
 *
 * @see Mediator
 * ZhangYi
 */
public class ConcrateMediator extends Mediator{

    /**
     * 同事类 集合 映射
     */
    private Map<String,Colleague> colleagueMap ;

    /**
     * 同事类名 与 name 映射
     */
    private Map<String,String> interMap ;

    public ConcrateMediator() {
        this.colleagueMap = new HashMap<>();
        this.interMap = new HashMap<>();
    }

    @Override
    public void registerMediator(String name,
                                 Colleague colleague) {
        colleagueMap.put(name,colleague);

        if (colleague instanceof TVColleague){
            interMap.put("TVColleague",name);
        }else if (colleague instanceof RefrigeratorColleague){
            interMap.put("RefrigeratorColleague",name);
        }else if (colleague instanceof CurtainsColleague){
            interMap.put("CurtainsColleague",name);
        }

    }

    /**
     * 功能描述: 接收同事的信息,并进行协调处理
     *
     * @param stateChange
     * @param name
     */
    @Override
    public void getMessage(int stateChange,
                           String name) {
        TVColleague tvColleague = (TVColleague) colleagueMap.get(interMap.get("TVColleague"));
        RefrigeratorColleague refrigeratorColleague =(RefrigeratorColleague)colleagueMap.get(interMap.get("RefrigeratorColleague"));
        CurtainsColleague curtainsColleague =(CurtainsColleague)colleagueMap.get(interMap.get("CurtainsColleague"));
        if (colleagueMap.get(name) instanceof TVColleague){
            // 电视机打开的情况下
            if (stateChange == 1){
                refrigeratorColleague.close(); // 关闭冰箱
                curtainsColleague.closeCurtains();
            }else if (stateChange == 0){
                refrigeratorColleague.open(); // 打开冰箱
            }
        }else if (colleagueMap.get(name) instanceof CurtainsColleague){
            if (stateChange == 1){
                tvColleague.startTV(); // 打开窗帘
            }else if (stateChange == 0){
                tvColleague.stopTV(); // 关闭窗帘
            }
        }else if (colleagueMap.get(name) instanceof RefrigeratorColleague){
            //if (stateChange == 1){
            //    curtainsColleague.openCurtains(); // 打开窗帘
            //}else if (stateChange == 0){
            //    curtainsColleague.closeCurtains(); // 关闭窗帘
            //}
        }

    }
}
